/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.lojaonlinefi.entity;
//import br.com.lojaonlinefi.entity.tipoenum.EstatusTipoDeVenda;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;

/**
 *
 * @author Carlos
 */
@Entity
@Table
public class Venda implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    @Basic(optional = false)
    private Integer id;
    
    @JoinColumn(referencedColumnName ="id", name = "usuario" )
    @ManyToOne(optional = false)
    @Valid
    private Usuario usuario;
    
    
    @JoinColumn(referencedColumnName = "id", name= "cartaoDeCredito")
    @ManyToOne(cascade = CascadeType.ALL, optional = false)
    @Valid
    private CartaoDeCredito cartaoDeCredito;
    
    @JoinColumn(referencedColumnName = "id", name = "enderecoDeEnvio")
    @ManyToOne(cascade = CascadeType.ALL, optional = false)
    private Endereco enderecoDeEnvio;
    
    @NotNull
    @NotEmpty    
    @Temporal(javax.persistence.TemporalType.DATE)
    @Column
    private Date dataDaVenda;
    
    @NotNull
    @NotEmpty
    @Column
    private BigDecimal total;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy="venda")
    private List<ItemVenda> itensVenda = new LinkedList<ItemVenda>();
    
    @Column
    private boolean fechada = false;
    
    // Adiciona um item de venda
    public void addItem(ItemVenda item){
        item.setVenda(this);
        getItensVenda().add(item);
        // Atulizando o total da venda
        if(total == null){
            total = item.getProduto().getValor();
        }else {
            // Devemos incrementar o valor do custo a cada novo item 
            total.add(item.getProduto().getValor());
        }
        
    }
    
    // Getters and Setters    
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

    public CartaoDeCredito getCartaoDeCredito() {
        return cartaoDeCredito;
    }

    public void setCartaoDeCredito(CartaoDeCredito cartaoDeCredito) {
        this.cartaoDeCredito = cartaoDeCredito;
    }

    public Endereco getEnderecoDeEnvio() {
        return enderecoDeEnvio;
    }

    public void setEnderecoDeEnvio(Endereco enderecoDeEnvio) {
        this.enderecoDeEnvio = enderecoDeEnvio;
    }

    public Date getDataDaVenda() {
        return dataDaVenda;
    }

    public void setDataDaVenda(Date dataDaVenda) {
        this.dataDaVenda = dataDaVenda;
    }

    public BigDecimal getTotal() {
        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }

    public boolean isFechada() {
        return fechada;
    }

    public void setFechada(boolean fechada) {
        this.fechada = fechada;
    }

    public List<ItemVenda> getItensVenda() {
        return itensVenda;
    }

    public void setItensVenda(List<ItemVenda> itensVenda) {
        this.itensVenda = itensVenda;
    }
    
    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Venda)) {
            return false;
        }
        Venda other = (Venda) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "br.com.lojaonlinefi.entity.Venda[ id=" + id + " ]";
    }
    
}
